package com.huluobo.high3videoproject.startLive.view;

import static com.tencent.live2.V2TXLiveCode.V2TXLIVE_ERROR_INVALID_LICENSE;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.base.view.BaseActivity;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.bean.StartLiveBeanResult;
import com.huluobo.high3videoproject.bean.StopLiveBeanResult;
import com.huluobo.high3videoproject.startLive.presenter.StartLiveContract;
import com.huluobo.high3videoproject.startLive.presenter.StartLivePresenter;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePlayer;
import com.tencent.live2.V2TXLivePusher;
import com.tencent.live2.impl.V2TXLivePlayerImpl;
import com.tencent.live2.impl.V2TXLivePusherImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class StartLiveActivity extends BaseActivity<StartLiveContract.View, StartLivePresenter> implements StartLiveContract.View {

    private TXCloudVideoView txPushView, txPullView;
    private Button startBtn, stopBtn;

    private EditText etCaption;

    private V2TXLivePusher v2TXLivePusher;
    private V2TXLivePlayer v2TXLivePlayer;

    @Override
    public void onStartLiveSuccess(StartLiveBeanResult startLiveBeanResult) {
        Log.i("TagA", "推流地址:" + startLiveBeanResult.getPushUrl());
        Log.i("TagA", "拉流地址:" + startLiveBeanResult.getFlvUrl());

        if (startLiveBeanResult.getPushUrl() != null) {
            int ret = v2TXLivePusher.startPush(startLiveBeanResult.getPushUrl());
            if (ret == V2TXLIVE_ERROR_INVALID_LICENSE) {
                Log.i("TagA", "startRTMPPush: license 校验失败");
            }
        }

        if (startLiveBeanResult.getFlvUrl() != null) {
            int code = v2TXLivePlayer.startLivePlay(startLiveBeanResult.getFlvUrl());
            Log.i("TagA", "拉流code:" + code);
        }
    }

    @Override
    public void onStartLiveFailed(String error) {
        ToastUtils.showLong("开启直播错误:" + error);
    }

    @Override
    public void onStopLiveSuccess(StopLiveBeanResult stopLiveBeanResult) {
        ToastUtils.showLong("下播成功!");
    }

    @Override
    public void onStopLiveFailed(String error) {
        ToastUtils.showLong("下播失败:" + error);
    }

    @Override
    protected void initData() {
        presenter = new StartLivePresenter();
        //推流组件
        v2TXLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);
        v2TXLivePusher.setRenderView(txPushView);

        v2TXLivePusher.startCamera(true);
        v2TXLivePusher.startMicrophone();

        //拉流组件
        v2TXLivePlayer = new V2TXLivePlayerImpl(this);
        v2TXLivePlayer.setRenderView(txPullView);

    }

    @Override
    protected void initView() {
        txPushView = findViewById(R.id.tx_video_view_push);
        txPullView = findViewById(R.id.tx_video_view_pull);
        startBtn = findViewById(R.id.btn_start_live);
        stopBtn = findViewById(R.id.btn_stop_live);
        etCaption = findViewById(R.id.ed_caption);

        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startLive();
            }
        });

        stopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopLive();
            }
        });
    }

    private void stopLive() {
        if (v2TXLivePusher != null) {
            v2TXLivePusher.stopPush();//关闭推流
            v2TXLivePusher.stopCamera();//关闭相机
            v2TXLivePusher.stopMicrophone();//关闭录音
        }

        //暂停拉流
        if (v2TXLivePlayer != null) {
            v2TXLivePlayer.pauseVideo();
            v2TXLivePlayer.pauseAudio();
            v2TXLivePlayer.stopPlay();
        }

        //调用接口,下播
        presenter.stopLive();
    }

    private void startLive() {
        String caption = etCaption.getText().toString();
        String address = "rtmp://188956.push.tlivecloud.com/TXLiveDemo/Test?txSecret=603f0a5ebd7261a254484687838ac84b" +
                "&txTime=64EDB020";
        if (!TextUtils.isEmpty(caption)) {
            presenter.startLive(address, caption);
        } else {
            ToastUtils.showLong("请输入直播描述信息");
        }
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_start_live;
    }
}